package com.baselibrary.libs.base.baseAdapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.baselibrary.libs.Utils.log.LogUtils;

import java.util.List;

/**
 * Created by 35876 于萌萌
 * 创建日期: 16:00 . 2016年10月08日
 * 描述:
 * <p>
 * <p>
 * 备注:
 */
public abstract class BaseListViewAdapter<T> extends BaseAdapter {
    protected Context mContext;
    protected LayoutInflater mInflater;
    protected List<T> mDatas;
    int type;

    public BaseListViewAdapter(Context context, int type, List<T> datas) {
        this.type = type;
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mDatas = datas;
    }



    public void setData(List<T> data) {
        mDatas = data;
        notifyDataSetChanged();
    }

    public void addData(List<T> data) {
        mDatas.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public T getItem(int i) {
        return mDatas.get(i);
    }

    @Override
    public int getItemViewType(int position) {
       return getViewType(mDatas.get(position),position);

    }

    /**
     * 基类不知道 你有什么样的布局 所以是抽象的
     * @param t
     * @param position
     * @return
     */
    public abstract int getViewType(T t ,int position);


    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        LogUtils.i("目前加载的条目是"+position);
         MyViewHolder myViewHolder = getViewHolder(position, convertView,
                parent);
        if (position<mDatas.size()) {
            convert(myViewHolder, mDatas.get(position), position);
        }
        return myViewHolder.getConvertView();

    }

    public abstract void convert(MyViewHolder helper, T item, int position);

    private MyViewHolder getViewHolder(int position, View convertView,
                                       ViewGroup parent)
    {

        int itemViewType = getItemViewType(position);

        return MyViewHolder.get(mContext,convertView, type,parent, itemViewType,
                position);
    }
}
